package com.sgb.kjwl.view.ui.adapter.self.carMasters;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.MyOfferEntity2;
import com.sgb.kjwl.utils.CargoConstant;
import com.sgb.kjwl.utils.NumberUtil;
import com.swgk.core.util.DateUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * @author: yangLei
 * @date: 2021/4/7 15:22
 * @Description: 我的报价适配器
 */
public class MyOfferAdapter extends BaseQuickAdapter<MyOfferEntity2, BaseViewHolder> {
    private RightBtnListener btnListener;
    private CheckOrderListener checkListener;

    public MyOfferAdapter(@Nullable List<MyOfferEntity2> datas) {
        super(R.layout.item_my_offer_re, datas);
    }

    public RightBtnListener getBtnListener() {
        return btnListener;
    }

    public void setBtnListener(RightBtnListener btnListener) {
        this.btnListener = btnListener;
    }

    public void setCheckListener(CheckOrderListener checkListener) {
        this.checkListener = checkListener;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder viewHolder, MyOfferEntity2 entity) {
        TextView offer_place_s = viewHolder.getView(R.id.offer_place_s);
        TextView offer_place_e = viewHolder.getView(R.id.offer_place_e);
        TextView offer_goods = viewHolder.getView(R.id.offer_goods);
        TextView offer_goods_time = viewHolder.getView(R.id.offer_goods_time);
        TextView msg_price = viewHolder.getView(R.id.msg_price);
        TextView newest_freight_offer = viewHolder.getView(R.id.newest_freight_offer);
        TextView offer_price_time = viewHolder.getView(R.id.offer_price_time);
        TextView update_offer = viewHolder.getView(R.id.tv_update_offer);
        TextView check_order = viewHolder.getView(R.id.tv_check_order);
        ImageView offer_status = viewHolder.getView(R.id.offer_status);
        RelativeLayout bottom_bar = viewHolder.getView(R.id.bottom_bar);

        String address_s = "";

        if (!TextUtils.isEmpty(entity.getCityName())) {
            address_s += entity.getCityName() + " ";
        }
        if (!TextUtils.isEmpty(entity.getDistrictName())) {
            address_s += entity.getDistrictName() + " ";
        }
        offer_place_s.setText(TextUtils.isEmpty(address_s) ? "" : address_s);

        String address_e = "";
        if (!TextUtils.isEmpty(entity.getDestCityName())) {
            address_e += entity.getDestCityName() + " ";
        }
        if (!TextUtils.isEmpty(entity.getDestDistrictName())) {
            address_e += entity.getDestDistrictName() + " ";
        }
        offer_place_e.setText(TextUtils.isEmpty(address_e) ? "" : address_e);

        //货物信息
        String goodsInfo = "";
        goodsInfo += CargoConstant.getLastGoodsTypeName(entity.getGoodsTypeName(), entity.getSecondGoodsTypeName());
        if (!TextUtils.isEmpty(entity.getWeight())) {
            goodsInfo += "/" + NumberUtil.trim0(entity.getWeight()) + "吨";
        }
        if (!TextUtils.isEmpty(entity.getVolume())) {
            goodsInfo += "/" + NumberUtil.trim0(entity.getVolume()) + "方";
        }
        offer_goods.setText(TextUtils.isEmpty(goodsInfo) ? "/" : goodsInfo);

//        StringBuilder str = new StringBuilder();
        long startTime;
        long endTime;
        if (!TextUtils.isEmpty(entity.getShipmentTime())) {
//            str.append(DateUtils.formatDate(entity.getShipmentTime(), "yyyy/MM/dd"));
            startTime = Long.parseLong(entity.getShipmentTime());
        } else {
            startTime = 0;
        }
        if (!TextUtils.isEmpty(entity.getShipmentEndTime())) {
//            str.append("-").append(DateUtils.formatDate(entity.getShipmentEndTime(), "yyyy/MM/dd"));
            endTime = Long.parseLong(entity.getShipmentEndTime());
        } else {
            endTime = 0;
        }
        //装货时间
        String date = CargoConstant.stampFormatListLoadTime(true, startTime
                , entity.getStartPointTime(), endTime, entity.getEndPointTime());
        if (!TextUtils.isEmpty(date)) {
            offer_goods_time.setText(date);
        } else {
            offer_goods_time.setText("/");
        }

        //信息费
//        if (!TextUtils.isEmpty(entity.getMessagePrice())) {
//            msg_price.setText(NumberUtil.trim0(entity.getMessagePrice()) + "元");
//        } else {
//            msg_price.setText("/");
//        }

        //最新报价
        if (!TextUtils.isEmpty(entity.getFreight())) {
            newest_freight_offer.setText(NumberUtil.trim0(entity.getFreight()) + "元/吨");
        } else {
            newest_freight_offer.setText("/");
        }
        if (!TextUtils.isEmpty(entity.getRecentQuotationTime())) {
            offer_price_time.setText(DateUtils.formatDate(String.valueOf(entity.getRecentQuotationTime()), "yyyy/MM/dd HH:mm:ss"));
        } else if (!TextUtils.isEmpty(entity.getCreateTime())) {
            offer_price_time.setText(DateUtils.formatDate(String.valueOf(entity.getCreateTime()), "yyyy/MM/dd HH:mm:ss"));
        }

        update_offer.getBackground().setAlpha(255);
        update_offer.setOnClickListener(v -> {
            if (btnListener != null)
                btnListener.onRightBtnClick(entity);
        });
        check_order.setOnClickListener(view -> {
            if (checkListener != null)
                checkListener.onCheckOrderClick(entity);
        });

        //报价状态：1已报价  2已同意  3报价未同意 4超时未支付
        switch (entity.getOfferStatus()) {
            case 1://已报价
                offer_status.setImageResource(R.mipmap.owner_agree_waiting);
                offer_status.setVisibility(View.VISIBLE);
                bottom_bar.setVisibility(View.VISIBLE);
                //1）用户参加过报价 且 竞价货源处于竞价中状态，显示  更新报价 按钮
                //2）用户参加过报价 且 竞价货源已结束，置灰 更新报价 按钮
                update_offer.setVisibility(View.VISIBLE);
                check_order.setVisibility(View.GONE);

                // 判断竞价是否已结束 结束置灰按钮，不能点击
                if (TextUtils.isEmpty(entity.getGoodsEndTime()) || Long.parseLong(entity.getGoodsEndTime()) <= System.currentTimeMillis()) {
                    update_offer.getBackground().setAlpha(128);
                    update_offer.setClickable(false);
                }
                break;

            case 2://已同意
                offer_status.setImageResource(R.mipmap.owner_agreed);
                offer_status.setVisibility(View.VISIBLE);
                bottom_bar.setVisibility(View.VISIBLE);
                update_offer.setVisibility(View.GONE);
                check_order.setVisibility(View.VISIBLE);
                break;

            case 3://报价未同意
            default:
                offer_status.setImageResource(R.mipmap.owner_refuse);
                offer_status.setVisibility(View.VISIBLE);
                bottom_bar.setVisibility(View.GONE);
                break;
        }

    }

    public interface RightBtnListener {
        void onRightBtnClick(MyOfferEntity2 entity);
    }

    public interface CheckOrderListener {
        void onCheckOrderClick(MyOfferEntity2 entity);
    }
}
